package com.yayun.merchant.controller;

import com.yayun.common.annotation.Anonymous;
import com.yayun.common.annotation.Log;
import com.yayun.common.core.controller.BaseController;
import com.yayun.common.core.domain.AjaxResult;
import com.yayun.common.core.page.TableDataInfo;
import com.yayun.common.enums.BusinessType;
import com.yayun.common.utils.poi.ExcelUtil;
import com.yayun.merchant.domain.AuctionItem;
import com.yayun.merchant.service.IAuctionItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/merchant/auction")
public class AuctionItemController extends BaseController {
    @Autowired
    private IAuctionItemService auctionItemService;

    /**
     * 查询拍卖商品列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:list')")
    @GetMapping("/list")
    public TableDataInfo list(AuctionItem auctionItem) {
        startPage();
        List<AuctionItem> list = auctionItemService.selectAuctionItemList(auctionItem);
        return getDataTable(list);
    }

    /**
     * 导出拍卖商品列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:export')")
    @Log(title = "拍卖商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AuctionItem auctionItem) {
        List<AuctionItem> list = auctionItemService.selectAuctionItemList(auctionItem);
        ExcelUtil<AuctionItem> util = new ExcelUtil<>(AuctionItem.class);
        util.exportExcel(response, list, "拍卖商品数据");
    }

    /**
     * 获取拍卖商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(auctionItemService.selectAuctionItemById(id));
    }

    /**
     * 新增拍卖商品
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:add')")
    @Log(title = "拍卖商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AuctionItem auctionItem) {
        return toAjax(auctionItemService.insertAuctionItem(auctionItem));
    }

    /**
     * 修改拍卖商品
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:edit')")
    @Log(title = "拍卖商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuctionItem auctionItem) {
        return toAjax(auctionItemService.updateAuctionItem(auctionItem));
    }

    /**
     * 删除拍卖商品
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:remove')")
    @Log(title = "拍卖商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(auctionItemService.deleteAuctionItemByIds(ids));
    }

    /**
     * 开始拍卖
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:start')")
    @Log(title = "拍卖商品", businessType = BusinessType.UPDATE)
    @PutMapping("/start/{id}")
    public AjaxResult start(@PathVariable Long id) {
        return toAjax(auctionItemService.startAuction(id));
    }

    /**
     * 结束拍卖
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:end')")
    @Log(title = "拍卖商品", businessType = BusinessType.UPDATE)
    @PutMapping("/end/{id}")
    public AjaxResult end(@PathVariable Long id) {
        Long orderId = auctionItemService.endAuction(id);
        return AjaxResult.success("拍卖已结束", orderId);
    }

    /**
     * 取消拍卖
     */
    @PreAuthorize("@ss.hasPermi('merchant:auction:cancel')")
    @Log(title = "拍卖商品", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id) {
        return toAjax(auctionItemService.cancelAuction(id));
    }

    /**
     * 前台查询拍卖商品列表（无需权限）
     */
    @GetMapping("/front/list")
    public TableDataInfo frontList(AuctionItem auctionItem) {
        startPage();
        List<AuctionItem> list = auctionItemService.selectAuctionItemList(auctionItem);
        return getDataTable(list);
    }

    /**
     * 前台获取拍卖商品详细信息（无需权限）
     */
    @GetMapping("/front/{id}")
    public AjaxResult frontGetInfo(@PathVariable("id") Long id) {
        return success(auctionItemService.selectAuctionItemById(id));
    }

    /**
     * 前台获取正在进行的拍卖（无需权限）
     */
    @GetMapping("/front/ongoing")
    public TableDataInfo frontOngoing() {
        startPage();
        AuctionItem query = new AuctionItem();
        query.setStatus(1); // 正在进行的拍卖
        List<AuctionItem> list = auctionItemService.selectAuctionItemList(query);
        return getDataTable(list);
    }

    /**
     * 前台获取即将开始的拍卖（无需权限）
     */
    @GetMapping("/front/upcoming")
    public TableDataInfo frontUpcoming() {
        startPage();
        AuctionItem query = new AuctionItem();
        query.setStatus(0); // 即将开始的拍卖
        List<AuctionItem> list = auctionItemService.selectAuctionItemList(query);
        return getDataTable(list);
    }

    /**
     * 前台获取已结束的拍卖（无需权限）
     */
    @GetMapping("/front/ended")
    public TableDataInfo frontEnded() {
        startPage();
        AuctionItem query = new AuctionItem();
        query.setStatus(2); // 已结束的拍卖
        List<AuctionItem> list = auctionItemService.selectAuctionItemList(query);
        return getDataTable(list);
    }

    /**
     * 前台参与竞拍（需要登录）
     */
    @PostMapping("/front/bid/{id}")
    public AjaxResult frontPlaceBid(@PathVariable("id") Long id, @RequestBody Map<String, BigDecimal> bidInfo) {
        return success(auctionItemService.placeBid(id, bidInfo.get("price")));
    }

    /**
     * 前台设置开拍提醒（需要登录）
     */
    @PostMapping("/front/remind/{id}")
    public AjaxResult frontSetReminder(@PathVariable("id") Long id) {
        return success(auctionItemService.setAuctionReminder(id));
    }

    /**
     * 前台获取拍卖历史记录（无需权限）
     */
    @Anonymous
    @GetMapping("/front/history/{id}")
    public TableDataInfo frontGetHistory(@PathVariable("id") Long id) {
        startPage();
        return getDataTable(auctionItemService.getAuctionHistory(id));
    }

    /**
     * 前台结束拍卖（需要登录）
     */
    @PostMapping("/front/end/{id}")
    public AjaxResult frontEndAuction(@PathVariable("id") Long id) {
        try {
            Long orderId = auctionItemService.endAuction(id);
            return AjaxResult.success("拍卖已结束", orderId);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
} 